export const dueReportSlot = 'report'
export const otherTag = '其他调查相关资料'

export const enumPersonalTags = [
  { parentTag: '调查附件', tag: '生产经营场所使用权情况', required: false },
  { parentTag: '调查附件', tag: '生产经营相关资料', required: false },
  { parentTag: '调查附件', tag: '用于经营的银行账号及近六个月流水', required: false },
  { parentTag: '调查附件', tag: '个人简历', required: false },
  { parentTag: '调查附件', tag: '家庭资产情况及家庭情况', required: false },
  { parentTag: '调查附件', tag: '农业保险及政策支持情况', required: false },
  { parentTag: '调查附件', tag: otherTag, required: false },
]

export const enumCompanyTags = [
  { parentTag: '调查附件', tag: '法人代表授权书', required: false },
  { parentTag: '调查附件', tag: '法定代表人及实际控制人简历', required: false },
  { parentTag: '调查附件', tag: '近两年的财务报表', required: false },
  { parentTag: '调查附件', tag: '主要账户近六个月的现金流水', required: false },
  { parentTag: '调查附件', tag: '农业保险情况及政府支持情况', required: false },
  {
    parentTag: '调查附件',
    tag: '法定代表人（实际控制人）关联企业基础资料（营业执照、最新公司章程等）、近一个月的征信报告',
    required: false,
  },
  { parentTag: '调查附件', tag: otherTag, required: false },
]

export const enumSceneTagsIn = [
  { parentTag: '调查附件', tag: '现场音视频', required: false },
  { parentTag: '调查附件', tag: '调查图片', required: true },
  { parentTag: '尽调报告', tag: '尽调报告', required: true, slot: dueReportSlot },
]

export const enumSceneTagsOut = [
  { parentTag: '调查附件', tag: '现场音视频', required: true },
  { parentTag: '调查附件', tag: '调查图片', required: true },
  { parentTag: '尽调报告', tag: '尽调报告', required: true, slot: dueReportSlot },
]

export const isCompanyProject = relatedData => +relatedData.customer_type === 1
export const getTags = relatedData =>
  isCompanyProject(relatedData) ? enumCompanyTags : enumPersonalTags

export const getAttachTags = relatedData =>
  +relatedData.process_status === 0 ? enumSceneTagsIn : enumSceneTagsOut
export const hasSceneMediaFinish = (attachs, relatedData) => {
  const mediaTag = getAttachTags(relatedData)[0]
  const hasUpload = attachs.some(it => it.attachment_type === mediaTag.tag)
  return {
    finish: !mediaTag.required || hasUpload,
    hasUpload,
  }
}
export const hasSceneImageFinish = (attachs, relatedData) => {
  const imageTag = getAttachTags(relatedData)[1]
  const hasUpload = attachs.some(it => it.attachment_type === imageTag.tag)
  return {
    finish: !imageTag.required || hasUpload,
    hasUpload,
  }
}
export const hasSceneReportFinish = relatedData => {
  const reportTag = getAttachTags(relatedData)[2]
  const hasUpload = +relatedData.census_status > 0
  return {
    finish: !reportTag.required || hasUpload,
    hasUpload,
  }
}
export const hasAttachFinish = (attachs, relatedData) => {
  const tags = getTags(relatedData)
  const listObj = attachs.reduce((prev, curr) => {
    prev[curr.attachment_type] = true
    return prev
  }, {})
  return tags.every(tag => !tag.required || listObj[tag.tag])
}

export const getTagsStatus = (attachs, relatedData) => {
  const res = []
  res.push({
    ...getAttachTags(relatedData)[0],
    ...hasSceneMediaFinish(attachs, relatedData),
  })
  res.push({
    ...getAttachTags(relatedData)[1],
    ...hasSceneImageFinish(attachs, relatedData),
  })
  res.push({
    ...getAttachTags(relatedData)[2],
    ...hasSceneReportFinish(relatedData),
  })

  const listObj = attachs.reduce((prev, curr) => {
    prev[curr.attachment_type] = true
    return prev
  }, {})
  const tags = getTags(relatedData)
  for (const tag of tags) {
    const hasUpload = listObj[tag.tag]
    res.push({
      ...tag,
      hasUpload,
      finish: !tag.required || hasUpload,
    })
  }

  return res
}
